using System;
using System.Data;
using Common.Enumerations;
using Sanelib.DataOperation;
using Sanelib.DataOperation.Attribute;
using Sanelib.DataOperation.Base;
using Sanelib.Dto.Enumerations;

namespace Core.Domain.Model
{
    [EntityName("Contact")]
    public class Contact : AuditedEntity
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public NameTitle NameTitle { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string CompanyName { get; set; }
        public Designation Designation { get; set; }

        public override void From(IDataReader dataReader)
        {
            base.From(dataReader);
            LastName = dataReader.ReadNullSafeString("LastName");
            FirstName = dataReader.ReadNullSafeString("FirstName");
            NameTitle = NameTitle.FromValue(dataReader.ReadNullSafeString("NameTitleValue"));
            Email = dataReader.ReadNullSafeString("Email");
            Phone = dataReader.ReadNullSafeString("Phone");
            Mobile = dataReader.ReadNullSafeString("Mobile");
            CompanyName = dataReader.ReadNullSafeString("CompanyName");
            Designation = Designation.FromValue(dataReader.ReadNullSafeString("DesignationValue"));
        }

        public override void To(System.Data.SqlClient.SqlCommand cmd)
        {
            base.To(cmd);
            cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastName;
            cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstName;
            cmd.Parameters.Add("@NameTitleValue", SqlDbType.Char, 2).Value = BaseEnumeration.GetDbNullSafe(NameTitle);
            cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
            cmd.Parameters.Add("@Phone", SqlDbType.VarChar).Value = (object)Phone ?? DBNull.Value;
            cmd.Parameters.Add("@Mobile", SqlDbType.VarChar).Value = (object)Mobile ?? DBNull.Value;
            cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar).Value = (object)CompanyName ?? DBNull.Value;
            cmd.Parameters.Add("@DesignationValue", SqlDbType.VarChar).Value = BaseEnumeration.GetDbNullSafe(Designation);
        }
    }
}
